package com.ruoyi.project.cms.mm.task.controller;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.mm.task.domain.TaskInfo;
import com.ruoyi.project.cms.mm.task.service.TaskInfoService;
import com.ruoyi.project.system.user.controller.ProfileController;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * @author Administrator
 * @create 2020-05-17 11:01
 * @desc 任务控制类
 **/
@Controller
@RequestMapping("cms/mm/task")
public class TaskController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(ProfileController.class);

    @Autowired
    private TaskInfoService taskInfoService;

    @Autowired
    private IUserService userService;

    private String prefix = "cms/mm/task";
    @GetMapping()
    public String task() {
        return prefix + "/task";
    }
    /**
     * 执行查询操作,这是任务创建页面的，这个页面中看到的数据都是创建人是登录用户的数据
     * @param taskInfo
     * @return TableDataInfo
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TaskInfo taskInfo) {
        System.out.println("进入查询方法");
        //获取当前登录用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String loginName = user.getLoginName();
        //当前登录的人员就是任务创建的人员
        taskInfo.setClogname(loginName);
        startPage();
        List<TaskInfo> list = taskInfoService.selectTaskInfoList(taskInfo);
        return getDataTable(list);
    }
    /**
     * 跳转添加页面
     * @return
     */
    @GetMapping("/add")
    public String addTask(ModelMap map) {
        //获取所有的session
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<User> users = userService.selectUserList(new User());
        map.put("users",users);
        map.put("user",user);
        return prefix + "/add";
    }
    /**
     * 执行新增方法
     * @return
     */
    @PostMapping("/doAdd")
    @ResponseBody
    public AjaxResult doAdd(TaskInfo info) {
        System.out.println("进入执行新增方法");
        //在新增之前需要设置的内容
        //1获取当前登录的用户作为创建项目的用户
        info.setTask_status(0);
        info.setCreate_date(new Date());
        System.out.println(info);
        return toAjax(taskInfoService.insertTask(info));
    }
    /**
     * 删除功能
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        System.out.println("开始文件删除操作");
        try {
            return toAjax(taskInfoService.deleteTaskByTid(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
    /**
     * 跳转任务下发页面
     * @return
     */
    @GetMapping("/taskPublish")
    public String taskPublish() {
        return prefix + "/taskPublish";
    }

    /**
     * 跳转到detail页面，并将数据回显到detail页面
     * @param tid
     * @param mmap
     * @return
     */
//    @RequiresPermissions("monitor:operlog:detail")
    @GetMapping("/detail/{tid}")
    public String detail(@PathVariable("tid") Integer tid, ModelMap mmap)
    {
        mmap.put("taskInfo", taskInfoService.selectTaskInfoByTid(tid));
        return prefix + "/detail";
    }

    @PostMapping("/publishList")
    @ResponseBody
    public TableDataInfo publishList(TaskInfo taskInfo) {
        System.out.println("进入任务下发查询方法");
        System.out.println(taskInfo);
        //获取当前登录用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        System.out.println("当前用户为：" + user);
        //将当前用户赋值为执行人员
        taskInfo.setDlogname(user.getLoginName());
        startPage();
        List<TaskInfo> list = taskInfoService.selectTaskInfoList(taskInfo);
        return getDataTable(list);
    }

//    文件上传
    @PostMapping("/uploadFile")
    @ResponseBody
    public String uploadFile(MultipartFile file) {
        System.out.println("开始文件上传");
        try {
                if (!file.isEmpty()) {
                    String fi = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
                    System.out.println(fi);
                    return fi;
                }
            } catch (IOException e) {
                log.error("上传文件失败！", e);
            }
            return null;
    }

    //    跳转查看任务成果页面
    @GetMapping("/result")
    public String result() {
        return prefix + "/result";
    }

    @PostMapping("/resultList")
    @ResponseBody
    public TableDataInfo resultList(TaskInfo info) {
        System.out.println("进入成果查询");
        User user = (User) SecurityUtils.getSubject().getPrincipal();
//        查询条件：执行人为当前登录用户的，且任务状态不为2的
        info.setDlogname(user.getLoginName());
        info.setTask_status(0);
        startPage();
        List<TaskInfo> list = taskInfoService.selectByDlogAndStatus(info);
        return getDataTable(list);
    }
//    跳转成果提交页面
    @GetMapping("/resultApply/{tid}")
    public String resultApply(@PathVariable("tid")Integer tid,ModelMap map) {
        System.out.println("跳转成果提交页面");
        TaskInfo info = taskInfoService.selectTaskInfoByTid(tid);
        map.put("info",info);
        return prefix + "/resultApply";
    }

//    执行成果提交
    @PostMapping("/doResultApply")
    @ResponseBody
    public AjaxResult doResultApply(TaskInfo info) {
        System.out.println("执行成果提交");
//        将任务设置为完成
        info.setTask_status(1);
        return toAjax(taskInfoService.resultApply(info));
    }

//    进入编辑页面
    @GetMapping("/editTask/{tid}")
    public String editTask(@PathVariable("tid")Integer tid, ModelMap map) {
        TaskInfo info = taskInfoService.selectTaskInfoByTid(tid);
        List<User> users = userService.selectUserList(new User());
        map.put("users",users);
        map.put("info",info);
        return prefix + "/editTask";
    }

    @PostMapping("/doEditTask")
    @ResponseBody
    public AjaxResult doEditTask(TaskInfo info) {
        return toAjax(taskInfoService.updateTask(info));
    }
}
